package com.aorise.controller.bayonet;

import com.aorise.service.bayonet.BayonetStatisticsService;
import com.aorise.utils.Page;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/27
 * @Description:    卡口统计报表控制器
 */
@Slf4j
@RestController
@Api(description ="卡口统计报表控制器")
@RequestMapping("api/bayonetStatistics")
@RequiresPermissions("KKTJBBFW")
public class BayonetStatisticsController {

    @Autowired
    BayonetStatisticsService bayonetStatisticsService;

    /**
     * 卡口统计报列表
     * HTTP 方式：GET
     * API 路径：/api/bayonetStatistics/getBayonetStatistics
     * 方法名：getBayonetStatistics
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "卡口统计报列表", httpMethod = "GET", response = String.class, notes = "卡口统计报列表，提供卡口统计报表使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageSize", value = "页大小",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页索引",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "groupId", value = "分组ID",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/getBayonetStatistics", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getBayonetStatistics(String startTime,String endTime,String pageSize,String pageNum,String groupId) {
        log.debug("Request RESTful API:getBayonetStatistics");
        log.debug("startTime {}",startTime);
        log.debug("endTime {}",endTime);
        Page bayonetPage =null;
        try {
            bayonetPage =bayonetStatisticsService.getBayonetStatistics(startTime,endTime,pageSize,pageNum,groupId);
            return JsonResponseData.ofSuccess(bayonetPage);
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetStatisticsController. function:getBayonetStatistics...msg:getBayonetStatistics DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetStatisticsController. function:getBayonetStatistics...msg:getBayonetStatistics Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }

    /**
     * 导出卡口统计报表
     * HTTP 方式：GET
     * API 路径：/api/bayonetStatistics/exportBayonetStatistics
     * 方法名：exportBayonetStatistics
     * 方法返回类型：String
     *
     */
    @ApiOperation(value = "导出卡口统计报表", httpMethod = "GET", response = String.class, notes = "导出卡口统计报表，提供卡口统计报表信息使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "groupId", value = "分组ID",  paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/exportBayonetStatistics", method = RequestMethod.GET)
    public String exportBayonetStatistics(String startTime,String endTime,String groupId, HttpServletResponse response) {
        log.debug("Request RESTful API:exportBayonetStatistics");
        try {
            bayonetStatisticsService.exportBayonetStatistics(startTime,endTime,groupId,response);
            return new JsonResponseData(true,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), StatusDefine.SUCCESS,
                    StatusDefineMessage.GetMessage(StatusDefine.SUCCESS), null).toString();
        } catch (DataAccessException e) {
            log.error(
                    "controller:BayonetController. function:exportBayonet...msg:exportBayonet DataAccessException. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:BayonetController. function:exportBayonet...msg:exportBayonet Exception. error:"
                            + e.getMessage());
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
    }
    
}
